package com.weaver.qfengx;

import com.engine.hrm.cmd.matrix.biz.MatrixMaintComInfo;
import com.google.common.collect.Lists;
import weaver.crm.Maint.CustomerInfoComInfo;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.hrm.cachecenter.bean.RolemembersComInfo;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.company.SubCompanyComInfo;
import weaver.hrm.job.JobActivitiesComInfo;
import weaver.hrm.job.JobGroupsComInfo;
import weaver.hrm.job.JobTitlesComInfo;
import weaver.hrm.job.UseKindComInfo;
import weaver.hrm.location.LocationComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.systeminfo.systemright.CheckUserRight;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 人员组织相关工具类
 * Created by YeShengtao on 2020/11/11 14:57
 */
public class HrmUtils {

    /**
     * 获取当前会话的用户
     */
    public static User getCurUser(HttpServletRequest request, HttpServletResponse response) {
        return HrmUserVarify.getUser(request, response);
    }

    /**
     * 根据当前userid 构造用户
     * @param userid
     * @return
     */
    public static User getUser(int userid) {
        return User.getUser(userid, 0);
    }

    /**
     * 根据当前userid 构造用户
     * @param userid
     * @return
     */
    public static User getUser(String userid) {
        return User.getUser(Integer.parseInt(userid), 0);
    }


    /**
     * 获取当前部门的一级部门
     */
    public static String topDeptId(String id) {
        Map<String, String> map = DaoUtils.executeQueryToMap("select id, supdepid, departmentname from HrmDepartment where id = ?", id);
        if (Objects.isNull(map)) {
            return null;
        }
        // 递归查询所在一级部门
        while (!StringUtils.equals("0", StringUtils.val(map.get("supdepid")))) {
            map = DaoUtils.executeQueryToMap("select id, supdepid, departmentname from HrmDepartment where id = ?", StringUtils.val(map.get("supdepid")));
            if (Objects.isNull(map)) {
                return null;
            }
        }
        return StringUtils.val(map.get("id"));
    }

    /**
     * 获取当前部门的层级链路
     * @param deptId 当前层级
     * @param containsCancel 是否包含封存
     * @return
     */
    public static List<Map<String, String>> getTopDeptChain(String deptId, boolean containsCancel) {
        String appendSql = "";
        if (!containsCancel) { // 不包含
            appendSql = " and (CANCELED != 1 or CANCELED is null) ";
        }
        List<Map<String, String>> res = Lists.newArrayList();
        Map<String, String> map = DaoUtils.executeQueryToMap("select * from HrmDepartment where id = ?" + appendSql, deptId);
        if (Objects.isNull(map)) {
            return Lists.newArrayList();
        }
        res.add(map);
        // 递归查询所在一级部门
        while (!StringUtils.equals("0", StringUtils.val(map.get("supdepid")))) {
            map = DaoUtils.executeQueryToMap("select * from HrmDepartment where id = ?" + appendSql, StringUtils.val(map.get("supdepid")));
            if (Objects.isNull(map)) {
                return res;
            } else {
                res.add(map);
            }
        }
        return res;
    }

    /**
     * 获取当前部门的层级链路 仅id
     * @param deptId 当前层级
     * @param containsCancel 是否包含封存
     * @return
     */
    public static List<String> getTopDeptIdChain(String deptId, boolean containsCancel) {
        List<Map<String, String>> res = getTopDeptChain(deptId, containsCancel);
        return res.stream().map(x -> StringUtils.val(x.get("id"), "")).collect(Collectors.toList());
    }

    public static List<String> getTopDeptIdChain(String deptId) {
        return getTopDeptIdChain(deptId,false);
    }

    public static List<Map<String, String>> getTopDeptChain(String deptId) {
        return getTopDeptChain(deptId,false);
    }

    public static List<String> getToSubComIdChain(String deptId, boolean containsCancel) {
        List<Map<String, String>> res = getToSubComChain(deptId, containsCancel);
        return res.stream().map(x -> StringUtils.val(x.get("id"), "")).collect(Collectors.toList());
    }


    public static List<String> getToSubComIdChain(String deptId) {
        return getToSubComIdChain(deptId,false);
    }

    public static List<Map<String, String>> getToSubComChain(String subComId, boolean containsCancel) {
        String appendSql = "";
        if (!containsCancel) { // 不包含
            appendSql = " and (CANCELED != 1 or CANCELED is null) ";
        }
        List<Map<String, String>> res = Lists.newArrayList();
        Map<String, String> map = DaoUtils.executeQueryToMap("select * from hrmsubcompany where id = ?" + appendSql, subComId);
        if (Objects.isNull(map)) {
            return Lists.newArrayList();
        }
        res.add(map);
        // 递归查询所在一级部门
        while (!StringUtils.equals("0", StringUtils.val(map.get("SUPSUBCOMID")))) {
            map = DaoUtils.executeQueryToMap("select * from hrmsubcompany where id = ?" + appendSql, StringUtils.val(map.get("SUPSUBCOMID")));
            if (Objects.isNull(map)) {
                return res;
            } else {
                res.add(map);
            }
        }
        return res;
    }


    /**
     * 获取一级分部层级链路
     * @param subComId
     * @return
     */
    public static List<Map<String, String>> getToSubComChain(String subComId) {
        return getToSubComChain(subComId, false);
    }

    public static Map <String, Object> getCustomerUserInfo(String userid) {
        Map <String, Object> userInfo = new HashMap<String, Object>();
        try {
            CustomerInfoComInfo comInfo = new CustomerInfoComInfo();
            userInfo.put("id", userid);
            userInfo.put("name", comInfo.getCustomerInfoname(userid));
            userInfo.put("usertype","1");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userInfo;
    }

    /**
     * 清除所有人力相关缓存
     */
    public static void clearAllCache() {
        try {
            ResourceComInfo resourcecominfo           = new ResourceComInfo();
            DepartmentComInfo departmentComInfo       = new DepartmentComInfo();
            SubCompanyComInfo subCompanyComInfo       = new SubCompanyComInfo();
            JobActivitiesComInfo jobActivitiesComInfo = new JobActivitiesComInfo();
            JobTitlesComInfo jobTitlesComInfo         = new JobTitlesComInfo();
            JobGroupsComInfo jobGroupsComInfo         = new JobGroupsComInfo();
            LocationComInfo locationComInfo           = new LocationComInfo();
            UseKindComInfo useKindComInfo             = new UseKindComInfo();
            CheckUserRight checkUserRight             = new CheckUserRight();
            RolemembersComInfo rolemembersComInfo     = new RolemembersComInfo();
            MatrixMaintComInfo matrixMaintComInfo     = new MatrixMaintComInfo();
            resourcecominfo.removeResourceCache();
            departmentComInfo.removeCompanyCache();
            subCompanyComInfo.removeCompanyCache();
            jobActivitiesComInfo.removeJobActivitiesCache();
            jobTitlesComInfo.removeJobTitlesCache();
            jobGroupsComInfo.removeCompanyCache();
            locationComInfo.removeLocationCache();
            useKindComInfo.removeUseKindCache();
            checkUserRight.removeMemberRoleCache();
            checkUserRight.removeRoleRightdetailCache();
            rolemembersComInfo.removeCache();
            matrixMaintComInfo.removeCache();
        } catch (Exception ignored) {}
    }

}
